package cn.edu.sanxiau.www.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import cn.edu.sanxiau.www.dao.UserRoleDao;
import cn.edu.sanxiau.www.model.UserRole;
import frame.DBUtils.JdbcUtils;

public class UserRoleDaoImpl implements UserRoleDao {

    @Override
    public int addUserRoleByUserRole(UserRole userRole) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JdbcUtils.getConnection();
            String sql = "INSERT INTO  userrole (user_role_id,user_id,role_id) VALUES(?,?,?)";
//	            Statement
//	            prepareStatement
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userRole.getUser_role_id());
            pstmt.setInt(2, userRole.getUser_id());
            pstmt.setInt(3, userRole.getRole_id());


            int n = pstmt.executeUpdate();
            return n;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int deleteUserRoleByUser_id(int user_id) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = JdbcUtils.getConnection();

            String sql = "DELETE FROM userrole WHERE user_id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, user_id);

            int m = pstmt.executeUpdate();
            return m;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

}
